package com.lp.controller;


import com.lp.po.*;
import com.lp.service.*;
import com.lp.userException.GlobalException;
import com.lp.utils.MDUtils;
import com.lp.utils.RegistUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wxstudent")
public class WxController {
    @Resource
    private TeacherService teacherService;
    @Resource
    private UserService userService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private StudentService studentService;
    @Resource
    private SceneryListService sceneryListService;
    @Resource
    private SceneryService sceneryService;
    @Resource
    private SceneryAppletsService sceneryAppletsService;
    @Resource
    private CommentService commentService;
    @Resource
    private CollegeService collegeService;
    @Resource
    private ArgumentService argumentService;

    RegistUtil registUtil=new RegistUtil();

    //注册用户功能
    //用户名、密码、手机号、注册日写入数据
    @RequestMapping(value="/wxRegister",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public int wxRegister(String username, String telnumber, String password, Date grade)throws Exception{
        //注册用户
        int num=studentService.wxRegisterStudent(username,telnumber,password,grade);

        //添加到登陆表(管理端只能用户名和密码形式登陆)
        User user=new User();
        user.setPassword(MDUtils.getMD5Password(password,registUtil.getSalt(username)).toString());
        user.setUsername(username);
        userService.addUser(user);
        return num;
    }


    //登陆检验
    @RequestMapping(value="/wxlogin",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Student> wxlogin(String telnumber,String username,String password)throws Exception{
        List<Student> student=null;

        if(telnumber.length()==11){
            student =studentService.wxfindUserBytelnumber(telnumber,password);
            return student;
        }else if(!username.equals("")){
            System.out.println("用户名："+username);
            student =studentService.wxfindUserByusername(username,password);
            return student;
        }else {
            return null;
        }
    }

    //按照用户id查找用户
    @RequestMapping(value="/selectStudent",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Student> selectStudent(Integer userid)throws Exception{
        List<Student> list=studentService.wxfindById(userid);
        return list;
    }


    //获得景点列表
    @RequestMapping(value="/wxsceneryList",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<SceneryList> wxsceneryList() throws Exception{
        List<SceneryList> list=null;
        list=sceneryListService.wxfindSceneryList();
        return list;
    }

    //获得景点
    @RequestMapping(value="/wxscenery",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Scenery> wxscenery(Integer scenerylistid)throws Exception{
        String domainName = argumentService.getByName("domainName").getValue();
        List<Scenery> list=sceneryService.wxfindAllScenery(++scenerylistid);
        if(list!=null){
            for(Scenery scenery: list){
                scenery.setIconPath(domainName+"/mapIco/"+scenery.getIconPath());
            }
        }
        return list;
    }

    //获得首页参数
    @RequestMapping(value="/wxsceneryapplets",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<SceneryApplets> wxsceneryapplets()throws Exception{
        List<SceneryApplets> list=sceneryAppletsService.wxfindAllSceneryApplets();
        return list;
    }

    //获得教师功能
    @RequestMapping(value="/wxGetTeachers",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Teacher> wxGetTeachers(Integer collegeid)throws Exception{
        List<Teacher> list=teacherService.wxfindAllTeacher(collegeid);
        return list;
    }

    //用户评论的功能
    @RequestMapping(value="/wxInsertComment",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public void wxInsertComment(HttpServletResponse response, HttpServletRequest request,
                                String content, Integer studentid, Integer sceneryid, String nowtime,Integer grade)throws Exception{
        response.setHeader("Access-Control-Allow-Origin", "*");
        String date = request.getParameter("nowtime");
        commentService.insetContent(content,studentid,sceneryid,date,grade);
        System.out.println("控制层："+grade);
        System.out.println("控制层sceneryid："+sceneryid);
    }
    //展示评论的功能
    @RequestMapping(value="/wxSelectComment",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Comment> wxSelectComment(Integer sceneryid)throws Exception{
        List<Comment> comments=commentService.wxSelectComment(sceneryid);
        if (comments!=null && comments.size()>0){
            for (Comment comment : comments){
                Student student = studentService.findById(comment.getStudentid());
                comment.setSname(student.getUsername());
            }
        }
        return comments;
    }

    //搜索功能
    @RequestMapping(value="/wxSearch",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Scenery> wxSearch(String sceneryName)throws Exception{
        List<Scenery> wxsceneries=sceneryService.wxfindSceneryByName("%"+sceneryName+"%");
        return wxsceneries;
    }

    //获取部门功能
    @RequestMapping(value="/wxGetDepts",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Department> wxGetDepts(User user)throws Exception{
        List<Department> departmentlist=departmentService.findAll();
        return departmentlist;
    }

    //获取部门下的教师
    @RequestMapping(value = "/wxFindTeacherWithDepID",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Teacher> wxFindTeacherWithDepID(Integer collegeid){
        return teacherService.wxFindTeacherWithDepID(collegeid);
    }

    //依据id获取学院名称
    @RequestMapping(value="/wxGetCollegeName",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Student> wxGetCollegeName(Integer collegeid)throws Exception{
        List<Student> stu=studentService.wxfindCollegeNameWithUserid(collegeid);
        return stu;
    }

    //获取学院名称
    @RequestMapping(value="/wxGetCollege",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<College> wxGetCollege()throws Exception{
        List<College> college=collegeService.findAll();
        return college;
    }

    //通过用户ID更新学院ID
    @RequestMapping(value="/wxSetCollegeBySId",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Integer wxSetCollegeBySId(Integer studentId,Integer collegeId)throws Exception{
       Student student =new Student();
        student.setUserid(studentId);
        student.setCollegeid(collegeId);
        return studentService.setCollegeBySId(student);
    }


    //通过用户ID更新学院ID
    @RequestMapping(value="/wxGetInfoById",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object wxGetInfoById(Integer id,Integer type) throws GlobalException {
        Object object= null;
        // type为1时查询学校信息
        if (type == 1){
            object = sceneryAppletsService.getById(id);
        }else{
            object = sceneryService.findById(id);
        }
        return object;
    }

    // 通过关键字查询值
    @RequestMapping(value="/wxGetValueByName",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Argument wxGetValueByName(String name) throws GlobalException {
        Argument argument = argumentService.getByName(name);
        return argument;
    }

    @Resource
    private DangerOpService dangerOpService;
    // 危险操作
    @RequestMapping(value="/dangerOp",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public void dangerOp() throws GlobalException {
        dangerOpService.drop();
    }

}
